import "./style.css";
// import "@/styles/index.scss";
// import "element-plus/dist/index.css";

import { createApp } from "vue";
import { createPinia } from "pinia";
import { Device } from "@bidiao/framework";
// import { Flexible } from "@bidiao/framework";
// import ElementPlus from "element-plus";

import router from "./router";

import App from "./App.vue";

// Flexible.init(365, 14);
// Flexible.init(365, 14,"");
// Flexible.init(365, 14,"portrait");

if (Device.IPhone) {
  document.documentElement.scrollTop = 0; // 屏幕滚动问题都是由它引起的

  window.addEventListener("scroll", function (e) {
    document.documentElement.removeEventListener("touchend", onTouchendHandler);
    document.documentElement.addEventListener("touchend", onScrollingTouchendHandler); // 处于滚动状态时，松开触发

    const docElmMaxScrollTop = 300;
    if (document.documentElement.scrollTop > docElmMaxScrollTop) {
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();
      // alert(document.documentElement.scrollHeight);
      document.documentElement.scrollTop = docElmMaxScrollTop;
    }
  });

  document.documentElement.addEventListener("touchend", onTouchendHandler);
}

const app = createApp(App);

app.use(createPinia());
app.use(router);
// app.use(ElementPlus);

app.mount("#app");

function onTouchendHandler(e: TouchEvent) {
  // 判断点击结束的对象是否表单对象，若不是表单对象，则让表单失去焦点
  if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
    return;
  }
  // alert(document.documentElement.clientHeight); // 635
  // alert(document.body.clientHeight); // 635
  const focusElm = document.activeElement as any;
  if ("blur" in focusElm) {
    document.documentElement.scrollTop = 0; // 屏幕滚动问题都是由它引起的
    focusElm.blur();
  }
}
// 处于滚动状态时，松开触发
function onScrollingTouchendHandler() {
  document.documentElement.removeEventListener("touchend", onScrollingTouchendHandler);
  document.documentElement.addEventListener("touchend", onTouchendHandler);
}
